package interview.algorithms.string;

import org.junit.Test;

/**
 * @author fance
 * @date 18-5-17 上午9:39
 */
public class Solution {

    /**
     * 是否为变形词
     * @param s1
     * @param s2
     * @return
     */
    public boolean isDeformation(String s1,String s2) {
        if (s1 == null || s2 == null || s1.length() != s2.length()) {
            return false;
        }
        int[] map = new int[256];
        for (int i = 0; i < s1.length(); i++) {
            map[s1.charAt(i)]++;
        }
        for (int i = 0; i < s2.length(); i++) {
            if (map[s2.charAt(i)]-- == 0) {
                return false;
            }
        }
        return true;

    }

    /**
     * 旋转词
     * @param s1
     * @param s2
     * @return
     */
    public boolean isRotation(String s1, String s2) {
        if (s1 == null || s2 == null | s1.length() != s2.length()) {
            return false;
        }
        return (s1 + s1).contains(s2);
    }

    /**
     * 所有字符只出现一次  1.map 2.堆排序
     * @param s
     * @return
     */
    public boolean isUnique(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }
        boolean[] map = new boolean[256];
        for (int i = 0; i < s.length(); i++) {
            if (map[s.charAt(i)]) {
                return false;
            }
            map[s.charAt(i)] = true;
        }
        return true;
    }

    public void rotate(char[] chars, int k) {
        if (chars == null || k <= 0) {
            return;
        }
        int len = chars.length;
        k = k % len;
        reverse(chars,0,k - 1);
        reverse(chars,k,len - 1);
        reverse(chars,0,len - 1);
    }
    private void reverse(char[] a, int i,int j) {
        for (;i < j;i++,j--) {
            char tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }
    }

    /**
     * 有效的括号
     * @param s
     * @return
     */
    public boolean isValid(String s) {
        if (s == null || s.length() < 1) {
            return false;
        }
        int status = 0;
        for (int i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            if (cur != '(' && cur != ')') {
                return false;
            }
            if (cur == ')' && --status < 0) {
                return false;
            }
            if (cur == '(') {
                status++;
            }
        }
        return status == 0;
    }



    public int wordCnt(String s) {
        int word = 0;
        int cnt = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                word = 0;
            } else if (word == 0) {
                word = 1;
                cnt++;
            }
        }
        return cnt;
    }

    /**
     * 557. 反转字符串中的单词 III
     * @param s
     * @return
     */
    public String reverseWords(String s) {
        if (s == null || s.length() < 1) {
            return s;
        }
        String[] arr = s.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            stringBuilder.append(new StringBuilder(arr[i]).reverse());
            stringBuilder.append(" ");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }

    /**
     * 520. 检测大写字母
     * @param word
     * @return
     */
    public boolean detectCapitalUse(String word) {
        if (word == null || word.length() < 1) {
            return false;
        }
        return isAlllower(word) || isAllUpper(word) || isFirstUpper(word);
    }
    private boolean isAllUpper(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (!(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')) {
                return false;
            }
        }
        return true;
    }
    private boolean isAlllower(String s) {
            for (int i = 0; i < s.length(); i++) {
                if (!(s.charAt(i) >= 'a' && s.charAt(i) <= 'z')) {
                   return false;
                }
        }
        return true;
    }
    private boolean isFirstUpper(String s) {
        return isAllUpper(s.substring(0,1)) && isAlllower(s.substring(1));
    }
    @Test
    public void test() {
       /* String s = "abcd";
        char[] chars = s.toCharArray();
        rotate(chars,5);
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
        }
*/
      //  System.out.println(reverseWords("Let's take LeetCode contest"));
      //  System.out.println(detectCapitalUse("FlaG"));
        System.out.println(detectCapitalUse("USA"));

    }
}
